package com.aurora.base.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class JsonDateValueUtil implements JsonValueProcessor {

	private String datePattern = "yyyy-MM-dd HH:mm";

	public JsonDateValueUtil() {
		super();
	}

	public JsonDateValueUtil(String format) {
		super();
		this.datePattern = format;
	}

	@Override
	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		return this.process(value);
	}

	@Override
	public Object processObjectValue(String key, Object value,
			JsonConfig jsonConfig) {
		return this.process(value);
	}

	private Object process(Object value) {
		try {
			if (value instanceof Date) {
				SimpleDateFormat sdf = new SimpleDateFormat(this.datePattern,
						Locale.UK);
				Date d = (Date) value;
				return d.getTime();
			}
			return value == null ? "" : value.toString();
		} catch (Exception e) {
			return "";
		}

	}

	public String getDatePattern() {
		return this.datePattern;
	}

	public void setDatePattern(String pDatePattern) {
		this.datePattern = pDatePattern;
	}

}